<?php


namespace app\home\controller;


use app\home\model\Browse;
use app\home\model\Collect;
use app\home\model\Comment;
use app\home\model\Like;
use app\home\model\NewsIssued;
use app\home\model\WechatUser;
use think\Db;

/**
 * Class Dispatch
 * @package app\home\controller
 * 公司发文Issued
 */
class Dispatch extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->anonymous();
        $this->jssdk();
    }

    /**
     *  公司发文首页
     *  type 1：行政administrative，2：党建party，3工会union
     */
    public function index () {
        $Model = new NewsIssued();
        if(IS_POST) {
            $type = input('type');
            $length = input('len',0);
            $list = $Model->getDataList(2,$type,8,$length);
            if($list) {
                return $this->success("加载成功","",$list);
            }else {
                return $this->error("加载失败");
            }
        }else {
            $list = [
                'list_1' => $Model->getDataList(2,1,8),
                'list_2' => $Model->getDataList(2,2,8),
                'list_3' => $Model->getDataList(2,3,8),
            ];
            $banner = [
                'banner_1' => $Model->getDataList(1,1,3),
                'banner_2' => $Model->getDataList(1,2,3),
                'banner_3' => $Model->getDataList(1,3,3),
            ];
            $this->assign('list',json_encode($list));
            $this->assign('banner',json_encode($banner));
            return $this->fetch();
        }

    }

    /**
     *  公司发文详情
     */
    public function detail () {
        $id = input('id');
        $userId = session('userId');
        $Model = new NewsIssued();
        $is_exist = $Model->where('id',$id)->where('status','egt',0)->find();
        if(empty($is_exist)) {
            return $this->error("查询发文不存在，请从列表入口重新查看！",Url('Dispatch/index'));
        }
        if($userId != "visitor") {
            //判断流量量增加
            $map_browser = [
                'uid' => $userId,
                'aid' => $id,
                'type' => 1
            ];
            $res = Browse::where($map_browser)->find();
            if(!$res) {
                $Model->where('id',$id)->setInc('views');
            }
            $this->browser(1,$userId,$id);
        }
        $detail = $Model->get($id);
        if($detail['attachment']) {
            $attachment = json_decode($detail['attachment']);
            $files = [];
            foreach ($attachment as $value) {
                $files[] = Db::table('zm_file')->where(['id' => $value])->find();
            }
            $detail['attachment'] = $files;
        }else {
            $detail['attachment'] = [];
        }
        $map = [
            'type' => 1, //公司简讯浏览
            'aid' => $id,
        ];
        $browser = Browse::where($map)->field('id,aid,uid,create_time')->order('create_time asc')->select();
        foreach ($browser as $key => $value) {
            $user = WechatUser::where(['userid' => $value['uid']])->find();
            $value['username'] = "无该用户";
            $value['header'] = null;
            if($user) {
                $value['username'] = $user['name'];
                $value['header'] = $user['header'] ? $user['header'] : $user['avatar'];
            }
        }
        $detail['browser_user'] = $browser;
        //获取 文章点赞
        $likeModel = new Like();
        $like = $likeModel->getLike(1,$id,$userId);
        $detail['is_like'] = $like;
        //获取 评论
        $commentModel = new Comment();
        $comment = $commentModel->getComment(1,$id,$userId);
        $detail['comment'] = $comment;
        //获取 收藏
        $collectModel = new Collect();
        $collect = $collectModel->getCollect(1,$id,$userId);
        $detail['is_collect'] = $collect;
        $this->assign('detail',json_encode($detail));
        $this->assign('userid',$userId);

        return $this->fetch();
    }
}
